import { onLoad, onShow } from "@dcloudio/uni-app";
import { ref } from "vue"
import { getMyLocation } from "@/utils/position";
import { uniCheckUserIsLogin } from "@/utils/userLogin";
import { goToPage, showModal } from "@/utils/utils";
import { mySchoolStore } from '@/store';

const addressManage = uniCloud.importObject(`admin-address-manage`, { customUI: true });


export default {
	setup() {
		let isLoginEnd = false;   //是否登录已经完成
		const activeIndex = ref(0)
		const authorizedAddress = ref(false)
		const consoleLog = ref("")
		Promise.all([
			new Promise<boolean>((resolve) => {
				onLoad((options) => {
					console.log(`onLoad`)
					let force = false;
					if (options && options.reLogin === `true`) {
						force = true;
						console.log(`token 过期需要重新登录`)
					}
					resolve(force)
				})
			}),
			new Promise<void>(resolve => {
				onShow(async () => {
					console.log(`onShow`)
					onShowDoSomething();
					resolve();
				})
			})
		]).then(async ([force]) => {
			const loginResult = await uniCheckUserIsLogin(force);
			isLoginEnd = true;
			onShowDoSomething();
			console.log(loginResult)
		})

		async function onShowDoSomething() {

			if (!isLoginEnd) {
				return console.log(`xxxxxx=> 自动登录未完成，取消本次onShow需要做的事情 <=xxxxxx`)
			}
			const { status, results } = await getMyLocation();
			if (status !== 3) {
				return false
			}
			const { latitude, longitude } = results;
			const schoolStore = mySchoolStore()
			if (schoolStore.mySchool._id === ``) {
				console.log(`======> 当前用户未选择学校，我们尝试为他自动定位获取学校 <======`)
				try {
					const { myErrCode, data, errMsg } = await addressManage.userAddressToSchool(latitude, longitude)
					if (myErrCode !== 0) {
						return showModal({
							title: `异常提示`,
							content: errMsg,
							showCancel: false
						})
					}
					if (Object.keys(data.school).length === 0) {
						await showModal({
							title: `操作提示`,
							content: `您附近暂未接入任何学校，试试手动定位？`,
							showCancel: false
						})
						return goToPage({
							path: `/common/schoolSelect/schoolSelect`,
							navType: `push`
						})
					} else {
						schoolStore.setMySchool(data.school)
					}
				} catch (error) {
					console.error(`xxxxxx=> 自动定位并选择学校错误，错误信息如下： <=xxxxxx`)
					console.error(error)
					return showModal({
						title: `异常提示`,
						content: `访问过于火爆，请稍后再试`,
						showCancel: false
					})
				}

			} else {
				console.log(`======> 当前用户已经选择学校，不用自动定位 <======`)
			}

			authorizedAddress.value = true;
		}

		const goToHomePage = (activeIndex : 0 | 1) => {
			// if(!authorizedAddress.value){
			//     return console.log(`xxxxxx=> 当前用户未授权位置信息 <=xxxxxx`)
			// }
			const toggle : {
				0 : {
					path : string;
					navType : 'pushTab';
				};
				1 : {
					path : string;
					navType : 'replaceAll';
				};
			} = {
					0: {
						path: `/common/tabbar1/tabbar1`,
						navType: `pushTab`
					},
					1: {
						path: `/riderUser/pages/home/home`,
						navType: `replaceAll`
					}
				}
			// if(activeIndex==1){
			//     return showModal({
			//         title:`权限提示`,
			//         content:`您暂无权限，请联系客服认证`,
			//         showCancel:false
			//     })
			// }
			const navOptions = toggle[activeIndex];
			goToPage(navOptions);
		}


		return {
			authorizedAddress,
			activeIndex,
			goToHomePage,
			consoleLog
		}
	}
}